java swing之自定义组件外观

首先看一下系统实现的外观效果
微博数据采集系统
可以看到自定义的外观简洁而风格统一

JFrame的自定义,继承JFrame

可以看到没有显示系统默认的标题栏,而是通过定义JPanel作为一个自定义标题栏,添加JLabel显示logo、软件名,添加JButton作为最小化按钮、关闭按钮,
最外侧的边框通过添加三个JLabel设置其背景色来实现

设置无标题栏

1
this.setUndecorated(true);

设置不可改变尺寸

1
this.setResizable(false);

设置为绝对布局

1
this.getContentPane().setLayout(null);

设置背景色

1
this.getContentPane().setBackground(new Color(249, 249, 249));

设置界面大小

1
2
3
4
5
6
7
8
//获取PC屏幕尺寸
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
screenWidth = screenSize.width;
screenHeight = screenSize.height;
//按屏幕尺寸固定比例设置软件尺寸
totalWidth = Maths.round(screenWidth * 0.95);
totalHeight = Maths.round(screenHeight * 0.95);
this.setSize(totalWidth, totalHeight);

设置初始位置

1
2
3
4
//获取任务栏高度,以便将软件位置初始化为屏幕正中央
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration());
int bottomInset = screenInsets.bottom;
this.setLocation(Maths.round((screenWidth - totalWidth) / 2), Maths.round((screenHeight - bottomInset - totalHeight) / 2));

添加子组件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//创建自定义标题栏
titleBarPanel = new JPanel();
titleBarPanel.setBounds(0, 0, totalWidth, titleBarPanelHeight);
titleBarPanel.setBackground(titleBarBackground);
this.getContentPane().add(titleBarPanel);
//创建自定义标题栏图标
int logoLeftMargin = 10;
titleBarPanel.setLayout(null);
double logoHeight = titleBarPanelHeight * 0.6;
ImageIcon logo = ImageUtil.scaleByHeight("image/logo.png", logoHeight);
JLabel logoLabel = new JLabel(logo);
logoLabel.setBounds(logoLeftMargin, Maths.round(titleBarPanelHeight * 0.2), logo.getIconWidth(), logo.getIconHeight());
titleBarPanel.add(logoLabel);
//创建自定义标题栏标题
JLabel titleLabel = new JLabel("新浪微博数据采集系统");
titleLabel.setFont(titleFont);
titleLabel.setForeground(titleColor);
int titleHeight = Maths.round(titleBarPanelHeight * 0.6);
titleLabel.setBounds(totalWidth / 2 - 180 / 2, Maths.round(titleBarPanelHeight * 0.2), 180, titleHeight);
titleBarPanel.add(titleLabel);
//创建自定义关闭按钮
JButton exitButton = ButtonFactory.get(ButtonFactory.J_EXIT);
int closeButtonWidth = exitButton.getIcon().getIconWidth();
int closeButtonHeight = exitButton.getIcon().getIconHeight();
exitButton.setBounds(totalWidth - borderMargin - closeButtonWidth, 0, closeButtonWidth, closeButtonHeight);
exitButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
UserInterface.this.dispose();
}
});
titleBarPanel.add(exitButton);
//创建自定义最小化按钮
JButton iconifyButton = ButtonFactory.get(ButtonFactory.J_ICONIFY);
int iconifyButtonWidth = iconifyButton.getIcon().getIconWidth();
int iconifyButtonHeight = iconifyButton.getIcon().getIconHeight();
iconifyButton.setBounds(totalWidth - borderMargin - iconifyButtonWidth - closeButtonWidth, 0, iconifyButtonWidth, iconifyButtonHeight);
iconifyButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
UserInterface.this.setExtendedState(JFrame.ICONIFIED);
}
});
titleBarPanel.add(iconifyButton);

给标题栏添加拖动移动界面效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//内部类
private class MyMouseAndMouseMotionListener extends MouseAdapter{
private Point pressed = null;
@Override
public void mousePressed(MouseEvent event) {
super.mousePressed(event);
if(event.getModifiers() == MouseEvent.BUTTON1_MASK){
int pressedX = event.getX();
int pressedY = event.getY();
if(pressedX >= 0 && pressedX <= totalWidth && pressedY >= 0 && pressedY <= titleBarPanelHeight){
pressed = event.getPoint();
}
else{
pressed = null;
}
}
}
@Override
public void mouseDragged(MouseEvent event) {
super.mouseDragged(event);
if(event.getModifiers() == MouseEvent.BUTTON1_MASK){
if(pressed != null){
Point dragged = event.getPoint();
Point location = UserInterface.this.getLocation();
int x = location.x + dragged.x - pressed.x;
int y = location.y + dragged.y - pressed.y;
UserInterface.this.setLocation(x, y);
}
}
}
}
//构造函数中
MyMouseAndMouseMotionListener listener = new MyMouseAndMouseMotionListener();
this.addMouseListener(listener);
this.addMouseMotionListener(listener);